//
//  HNSH_ShopApplyTopView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/4.
//

#import "HNSH_ShopApplyTopView.h"

@interface HNSH_ShopApplyTopItemView : UIView

/* toptopLabelImgV */
@property (nonatomic, strong) UILabel *topLabel;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* tapClick */
@property (nonatomic, copy) dispatch_block_t tapClick;

/* select */
@property (nonatomic, assign) BOOL select;
@end

@implementation HNSH_ShopApplyTopItemView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addSubview:self.topLabel];
    [self addSubview:self.titleLabel];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.width.height.mas_equalTo(36);
        make.top.mas_equalTo(0);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_equalTo(17);
        make.bottom.mas_equalTo(0);
    }];
    
    self.userInteractionEnabled = YES;
    WeakSelf(weakSelf);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.tapClick) {
            weakSelf.tapClick();
        }
    }];
    [self addGestureRecognizer:tap];
}

- (void)setSelect:(BOOL)select {
    _select = select;
    if (select) {
        self.topLabel.backgroundColor = [UIColor appOrangeLabelColor];
        self.titleLabel.textColor = [UIColor appOrangeLabelColor];
    } else {
        self.topLabel.backgroundColor = [UIColor colorWithHexString:@"#FDB38C"];
        self.titleLabel.textColor = [UIColor colorWithHexString:@"#FDB38C"];
    }
    
}

#pragma mark lazy
- (UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = [[UILabel alloc]init];
        _topLabel.textColor = [UIColor appWhiteLabelColor];
        _topLabel.font = [UIFont boldSystemFontOfSize:18];
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.backgroundColor = [UIColor colorWithHexString:@"#FDB38C"];
        _topLabel.layer.cornerRadius = 18.0f;
        _topLabel.layer.masksToBounds = YES;
    }
    return _topLabel;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#FDB38C"];
        _titleLabel.font = [UIFont boldSystemFontOfSize:12];
    }
    return _titleLabel;
}

@end

@interface HNSH_ShopApplyTopView ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* itemsAry */
@property (nonatomic, strong) NSMutableArray *itemsAry;
/* lineAry */
@property (nonatomic, strong) NSMutableArray *lineAry;

@end

@implementation HNSH_ShopApplyTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    
    CGFloat totalWidth = 0;
    for (NSString *title in titles) {
        totalWidth = totalWidth + [title widthForFont:[UIFont boldSystemFontOfSize:12]];
    }
    
    WeakSelf(weakSelf);
    //计算title之间的间隙
    CGFloat labelSpec = (self.bgView.width-40-totalWidth)/(titles.count-1);
    //计算线长
    CGFloat lineWidth = labelSpec + 10;
    
    self.itemsAry = [NSMutableArray arrayWithCapacity:titles.count];
    self.lineAry = [NSMutableArray arrayWithCapacity:titles.count-1];
    CGFloat currentX = 20;
    for (int i = 0; i < titles.count; i ++) {
        NSString *title = titles[i];
        UIFont *font = [UIFont boldSystemFontOfSize:12];
        CGFloat width = [title widthForFont:font];
        HNSH_ShopApplyTopItemView *item = [[HNSH_ShopApplyTopItemView alloc]initWithFrame:CGRectMake(currentX, 46, width, 59)];
        item.titleLabel.text = title;
        item.topLabel.text = [NSString stringWithFormat:@"%d",i+1];
        item.select = NO;
        if (i == 0) {
            item.select = YES;
        }
        [self.bgView addSubview:item];
        [self.itemsAry addObject:item];
        item.tapClick = ^{
//            [weakSelf changeIndexWithTapIndex:i];
        };
        
        currentX = currentX + width + labelSpec;
        if (i < titles.count - 1) {
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(currentX - labelSpec - 5, 64, lineWidth, 2)];
            lineView.backgroundColor = [UIColor colorWithHexString:@"#FDB38C"];
            lineView.layer.cornerRadius = 1;
            lineView.layer.masksToBounds = YES;
            [self.bgView addSubview:lineView];
            [self.lineAry addObject:lineView];
        }
    }
}

- (void)changeIndexWithTapIndex:(NSInteger)index {
    self.currentIndex = index;
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    for (int i = 0; i < self.itemsAry.count; i ++) {
        HNSH_ShopApplyTopItemView *item = self.itemsAry[i];
        if (currentIndex >= i) {
            item.select = YES;
        } else {
            item.select = NO;
        }
    }
    
    for (int i = 0; i < self.lineAry.count; i ++) {
        UIView *lineView = self.lineAry[i];
        if (currentIndex >= i) {
            lineView.backgroundColor = [UIColor orangeColor];
        } else {
            lineView.backgroundColor = [UIColor colorWithHexString:@"#FDB38C"];
        }
    }
}

- (void)configUI {
    [self addSubview:self.bgView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 12, 200, 20)];
    titleLabel.textColor = [UIColor appOrangeLabelColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = @"店铺申请";
    [self.bgView addSubview:titleLabel];
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(12, 12, kScreenWidth-24, 121)];
        _bgView.backgroundColor = [UIColor colorWithHexString:@"#FFBAB2"];
        _bgView.layer.cornerRadius = 8.0f;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = [[UIColor colorWithHexString:@"#FFBAB2"]CGColor];
        _bgView.layer.borderWidth = 1.0f;
    }
    return _bgView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
